﻿using System;
using System.Collections.Generic;
//using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using CardGamesLibrary.Games;
using CardGamesLibrary.Tables;

namespace CardGamesLibrary.Poker
{
    public class TexasHoldemPokerGame : PokerGameBase
    {
        private readonly PokerTable _table;

        public TexasHoldemPokerGame(PokerTable table) : base(table)
        {
           

            _table = table;
        }

        public override void CreateAndPlayNewTurn()
        {
            GameTurn = new PokerGameTurn(_table);
            while (!GameTurn.Finished)
            {
                GameTurn.Play();
            }
            Console.WriteLine(_table.CurrentPot);
            
            var winner =((PokerGameTurn)GameTurn).GetWinner() as PokerSeat;
            
            _table.Cards.Clear();
            
            foreach (var tableSeat in _table.Seats.Where(a=>a.Player != null))
            {
                tableSeat.Hand.Clear();
            }
            _table.Dealer.RenewDeck();

            var pot = _table.CurrentPot;
            _table.CurrentPot = 0;
            winner.Chips += pot;
            foreach (var item in _table.Seats.OfType<PokerSeat>())
            {
                if (item.Player != null && item.Chips <= 0)
                    item.Player = null;
            }
        }

       
    }
}
